package com.leantech.android.browser.dialog

import android.content.Context
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.text.buildSpannedString
import com.leantech.android.browser.databinding.DialogLoginConfirmBinding
import com.leantech.android.browser.utils.UrlClickableSpan
import com.leantech.android.browser.utils.UrlProvider
import com.leantech.android.browser.utils.onThrottleClick
import com.cscj.android.utils.SpanUtils
import com.leantech.android.browser.R
import com.qmuiteam.qmui.skin.QMUISkinManager
import com.qmuiteam.qmui.widget.dialog.QMUIBaseDialog
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import splitties.resources.color

class LoginConfirmDialog(
    private val context: Context, onAgree: () -> Unit = {}, onDisagree: () -> Unit = {}
) : QMUIBaseDialog(context, R.style.MessageDialogStyle), KoinComponent {

    private val urlProvider by inject<UrlProvider>()

    init {
        val binding = DialogLoginConfirmBinding.inflate(LayoutInflater.from(context))
        setSkinManager(QMUISkinManager.defaultInstance(context))
        setContentView(
            binding.root, ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
            )
        )

        binding.content.movementMethod = LinkMovementMethod.getInstance()
        binding.content.text = buildPrivacyContent()
        binding.buttonGroup.btnSure.onThrottleClick {
            onAgree()
            dismiss()
        }

        binding.buttonGroup.btnCancel.onThrottleClick {
            onDisagree()
            dismiss()
        }
    }

    private fun buildPrivacyContent(): CharSequence {
        return buildSpannedString {
            append("我已阅读并同意")
            append(
                SpanUtils.makeSpan(
                    "《服务协议》", UrlClickableSpan(
                        urlProvider.getServiceUrl(), context.color(
                            R.color.primaryBlue
                        ), "服务协议"
                    )
                )
            )
            append("和")
            append(
                SpanUtils.makeSpan(
                    "《隐私协议》", UrlClickableSpan(
                        urlProvider.getPrivacyUrl(), context.color(
                            R.color.primaryBlue
                        ), "隐私协议"
                    )
                )
            )
        }

    }


}